package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.mine.model.UserAsset;
import com.meitianhui.convenitenbuy.module.wallet.adapter.WalletGoldAdapter;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletGoldContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.PropertyBean;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletGoldPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletGoldActivity extends BaseToolbarActivity implements WalletGoldContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    TextView mTvGoldNum;
    RecyclerView mRv;
    LinearLayout mLlNone;
    private int pageNum = 1;
    @Inject
    WalletGoldPresenter mPresenter;
    private BaseQuickAdapter mAdapter;
    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);
        initId();
        setTbTitle(getString(R.string.wallet_gold_coins));
        initGoldInfo();
        registerPush();
    }

    private void registerPush() {
        Disposable disposable = RxBus.get().toFlowable(EventBean.class).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean bean) throws Exception {
                if (bean != null) {
                    if (bean.type == Constants.EVENT_TYPE_REFRESH) {
                        initGoldInfo();
                    }
                }
            }
        });
        mCompositeDisposable.add(disposable);
    }

    private void initGoldInfo() {
        getGold();
        getGoldDetail();
    }

    private void getGold() {
        mPresenter.getGold();
    }

    private void initId() {
        mTvGoldNum = getViewById(R.id.tv_wallet_gold_num);
        mRv = getViewById(R.id.rv_wallet_gold);
        mLlNone = getViewById(R.id.ll_wallet_gold_none);
        LinearLayoutManager mLinearManager = new LinearLayoutManager(this);
        mLinearManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(mLinearManager);
    }

    private void getGoldDetail() {
        mPresenter.getGoldRecord(pageNum);
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_gold;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    protected void onRefreshData() {
        if (mAdapter != null) {
            if (mAdapter.isLoading()) mAdapter.loadMoreEnd();
        }
        pageNum = 1;
        mPresenter.getGoldRecord(pageNum);
    }

    @Override
    public void getGoldRecordBack(List<PropertyBean> list) {
        if (mAdapter == null) {
            mAdapter = new WalletGoldAdapter(this, list);
            mRv.setAdapter(mAdapter);
            mAdapter.setOnLoadMoreListener(this, mRv);
        } else {
            if (mAdapter.isLoading()) mAdapter.loadMoreComplete();
            if (pageNum == 1) {
                mAdapter.replaceData(list);
            } else {
                mAdapter.addData(list);
            }
        }
        if (mAdapter.isLoading()) mAdapter.loadMoreComplete();
        mAdapter.setEnableLoadMore(list.size() < 20 ? false : true);
        mLlNone.setVisibility(list.isEmpty() && pageNum == 1 ? View.VISIBLE : View.GONE);
        refreshFinish();
    }

    @Override
    public void getGoldBack(UserAsset bean) {
        if (bean != null) {
            mTvGoldNum.setText(TextUtils.isEmpty(bean.getGold()) ? "0" : bean.getGold());
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter != null && mAdapter.isLoading()) mAdapter.loadMoreEnd();
        refreshFinish();
    }

    @Override
    public void onLoadMoreRequested() {
        pageNum++;
        getGoldDetail();
    }


}
